﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Red_Cross_Philippines;

namespace Philippine_National_Red_Cross
{
    public partial class frmChangePassword : Form
    {
        Blood_Bank_System BloodSystem = new Blood_Bank_System();
        public DataTable tbl = new DataTable();
        int i = 0;

        string username, password;
        public frmChangePassword(string user, string pass)
        {
            this.username = user;
            this.password = pass;

            InitializeComponent();
            this.Load += new EventHandler(frmChangePassword_Load);
            btnChangePass.Click += new EventHandler(btnChangePass_Click);
        }

        private void ChangePassword()
        {
            BloodSystem.Query("SELECT * FROM tblusers WHERE Password LIKE '%" + password + "%'");
            if (BloodSystem.tbl.Rows.Count > 0)
            {
                tbl = BloodSystem.tbl;
                Change_Password chpassword = new Change_Password();
                chpassword.passwordUp = txtNewPassword.Text;
                BloodSystem.updatePassword(chpassword, i);
            }
        }

        void btnChangePass_Click(object sender, EventArgs e)
        {
            if (txtOldPassword.Text == "")
            {
                MessageBox.Show("Please input your old password", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtOldPassword.Focus();
                return;
            }
            if (txtOldPassword.Text != password)
            {
                MessageBox.Show("Incorrect Password", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtOldPassword.Focus();
                return;
            }
            if (txtNewPassword.Text == "")
            {
                MessageBox.Show("Please input your new password", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtNewPassword.Focus();
                return;
            }
            else if (txtConfirmPassword.Text != txtNewPassword.Text)
            {
                MessageBox.Show("Password not match", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtConfirmPassword.Focus();
                txtConfirmPassword.Clear();
                return;
            }
            ChangePassword();
            MessageBox.Show("You have successfully changed your password", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            txtNewPassword.Clear();
            txtOldPassword.Clear();
            txtConfirmPassword.Clear();
        }

        void frmChangePassword_Load(object sender, EventArgs e)
        {
            BloodSystem.Connect();
            BloodSystem.Disconnect();
        }
    }
}
